// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <nebula/types/fundamental.h>

namespace nebula {

    // ----------------------------------------------------------------------
    // Dictionary type (for representing categorical or dictionary-encoded
    // in memory)

    /// \brief Dictionary-encoded value type with data-dependent
    /// dictionary. Indices are represented by any integer types.
    class TURBO_EXPORT DictionaryType : public FixedWidthType {
    public:
        static constexpr Type::type type_id = Type::DICTIONARY;

        static constexpr const char *type_name() { return "dictionary"; }

        DictionaryType(const std::shared_ptr<DataType> &index_type,
                       const std::shared_ptr<DataType> &value_type, bool ordered = false);

        // A constructor variant that validates its input parameters
        static turbo::Result<std::shared_ptr<DataType>> create(
                const std::shared_ptr<DataType> &index_type,
                const std::shared_ptr<DataType> &value_type, bool ordered = false);

        std::string to_string(bool show_metadata = false) const override;

        std::string name() const override { return "dictionary"; }

        int bit_width() const override;

        DataTypeLayout layout() const override;

        const std::shared_ptr<DataType> &index_type() const { return index_type_; }

        const std::shared_ptr<DataType> &get_value_type() const { return value_type_; }

        bool ordered() const { return ordered_; }

    protected:
        static turbo::Status ValidateParameters(const DataType &index_type,
                                                const DataType &value_type);

        std::string compute_fingerprint() const override;

        // Must be an integer type (not currently checked)
        std::shared_ptr<DataType> index_type_;
        std::shared_ptr<DataType> value_type_;
        bool ordered_;
    };

}  // namespace nebula